package com.xiaotu.mvc.config;

import com.google.gson.Gson;
import com.xiaotu.constant.CrowdConstant;
import com.xiaotu.exception.EditUserFaildException;
import com.xiaotu.exception.LoginFailedException;
import com.xiaotu.util.CrowdUtil;
import com.xiaotu.util.ResultEntity;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@ControllerAdvice
public class CrowdExceptionResolver {
    //@ExceptionHandler将一个具体的异常类型和一个方法关联起来
    @ExceptionHandler(NullPointerException.class)
    public ModelAndView resolveNullPointerException(
            //实际捕获到的异常类型
            NullPointerException e,
            //当前请求对象
            HttpServletRequest httpServletRequest,
            //当前响应对象
            HttpServletResponse httpServletResponse
    ) throws IOException {
        String viewName = "system-error";
        return commonResolver(viewName, e, httpServletRequest, httpServletResponse);
    }
    @ExceptionHandler(EditUserFaildException.class)
    public ModelAndView resolveNullPointerException(
            //实际捕获到的异常类型
            EditUserFaildException e,
            //当前请求对象
            HttpServletRequest httpServletRequest,
            //当前响应对象
            HttpServletResponse httpServletResponse
    ) throws IOException {
        String viewName = "system-error";
        return commonResolver(viewName, e, httpServletRequest, httpServletResponse);
    }
    @ExceptionHandler(LoginFailedException.class)
    public ModelAndView resolveLoginFailedException(
            LoginFailedException loginFailedException,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) throws IOException {
        String viewName = "admin-login";
        return commonResolver(viewName, loginFailedException, httpServletRequest, httpServletResponse);
    }
    private ModelAndView commonResolver(
            //异常处理完成后要去的页面
            String viewName,
            Exception exception,
            HttpServletRequest httpServletRequest ,
            HttpServletResponse httpServletResponse) throws IOException {
        //判断当前请求类型
        boolean judgeRequestType = CrowdUtil.judgeRequestType(httpServletRequest);
        //如果是Ajax请求
        if (judgeRequestType){
            //创建ResultEntity对象
            ResultEntity<Object> fail = ResultEntity.fail(exception.getMessage());
            //创建Gson对象,将entity对象转换为json字符串
            String json = new Gson().toJson(fail);
            //将json字符串作为响应题返回给浏览器
            httpServletResponse.getWriter().write(json);
            //
            return null;
        }
        //如果不是Ajax请求,则创建MOdelandView 对象
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(CrowdConstant.ATTR_NAME_EXCEPTION,exception);
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
}
